import {
	IParam,
	IUIActionResult,
	IActionModel,
	IContext,
	IAbility,
	IHttpResponse,
	deepCopy,
	IViewAbility,
	srfpluralize,
	hasFunction,
	formatUrlString,
	IRedirectInput,
	transformDoData,
} from '@/core';
import { Http } from '@/http';
import { EntityUIServiceBase } from '../entity-ui-service-base';
import UnAuthUILogic from './un-auth-ui-logic';

/**
 * 系统角色成员UI服务对象基类
 * 基于template/src/ui-service/{{appEntities}}/{{appEntities}}-ui-service-base.ts.hbs生成
 *
 * @export
 * @class Sys_role_memberUIServiceBase
 * @extends {UIService}
 */
export class Sys_role_memberUIServiceBase extends EntityUIServiceBase {
	/**
	 * Creates an instance of Sys_role_memberUIServiceBase
	 *
	 * @param {*} [opts={}]
	 * @memberof Sys_role_memberUIServiceBase
	 */
	constructor(opts: IParam = {}) {
		super(opts);
		this.appDeKeyFieldName = 'id';
		// 是否启用主状态
		this.isEnableDEMainState = true;
		// 主状态属性集合
		this.mainStateFields = ['member_type'];
		// 主状态集合Map
		this.allDeMainStateMap.set('organizationalRole', 'organizationalRole');
		this.allDeMainStateMap.set('inetOrgPerson', 'inetOrgPerson');
		// 主状态操作标识Map
		this.allDeMainStateOPPrivsMap.set(
			'organizationalRole',
			Object.assign(
				{ CREATE: 1, DELETE: 1, READ: 1, UNAUTH: 1, UPDATE: 1 },
				{ UNAUTH: 0 }
			)
		);
		this.allDeMainStateOPPrivsMap.set(
			'inetOrgPerson',
			Object.assign({ CREATE: 1, DELETE: 1, READ: 1, UNAUTH: 1, UPDATE: 1 }, {})
		);
		this.allViewMap.set(':', {
			viewName: 'sys_role_memberPersonGridView',
			viewType: 'DEGRIDVIEW',
			srfappde: 'sys_role_members',
			deCodeName: 'Sys_role_member',
		});
		this.allViewMap.set(':', {
			viewName: 'sys_role_memberUsr0310377617GridView',
			viewType: 'DEGRIDVIEW',
			srfappde: 'sys_role_members',
			deCodeName: 'Sys_role_member',
		});
		this.allViewMap.set(':', {
			viewName: 'sys_role_memberGroupGridView',
			viewType: 'DEGRIDVIEW',
			srfappde: 'sys_role_members',
			deCodeName: 'Sys_role_member',
		});
		this.allViewMap.set('REDIRECTVIEW:', {
			viewName: 'sys_role_memberRedirectView',
			viewType: 'DEREDIRECTVIEW',
			srfappde: 'sys_role_members',
			deCodeName: 'Sys_role_member',
		});
		this.allViewMap.set('EDITVIEW:', {
			viewName: 'sys_role_memberEditView',
			viewType: 'DEEDITVIEW',
			srfappde: 'sys_role_members',
			deCodeName: 'Sys_role_member',
		});
	}

	/**
	 * 收回授权
	 *
	 * @param {*} params
	 * @memberof Sys_role_memberUIServiceBase
	 */
	public async groupUnAuth(
		uiContext: IContext,
		uiViewParams: IParam,
		uiData: IParam[],
		event: MouseEvent,
		ability: IAbility
	): Promise<IUIActionResult> {
		//	替换执行界面逻辑
		return await this.executeUILogic(
			'unAuth',
			uiContext,
			uiViewParams,
			uiData,
			event,
			ability
		);
	}

	/**
	 * 收回授权
	 *
	 * @param {*} params
	 * @memberof Sys_role_memberUIServiceBase
	 */
	public async personUnAuth(
		uiContext: IContext,
		uiViewParams: IParam,
		uiData: IParam[],
		event: MouseEvent,
		ability: IAbility
	): Promise<IUIActionResult> {
		//	替换执行界面逻辑
		return await this.executeUILogic(
			'unAuth',
			uiContext,
			uiViewParams,
			uiData,
			event,
			ability
		);
	}

	/**
	 * 获取指定数据的重定向页面
	 *
	 * @param context 应用上下文
	 * @param viewParams 视图参数
	 * @param data 业务数据
	 * @param redirectParam 重定向视图输入参数
	 * @memberof Sys_role_memberUIServiceBase
	 */
	public async getAppRDView(
		context: IContext,
		viewParams: IParam,
		data: IParam,
		redirectParam: IRedirectInput
	) {
		// 进行数据查询
		const service = await App.getDataService('Sys_role_member', context);
		const tempContext: IContext = deepCopy(context);
		const key: string = 'sys_role_member';
		let srfkey: string | null = null;
		if (data && data.hasOwnProperty(key)) {
			srfkey = data[key];
		} else if (context && context[key]) {
			srfkey = context[key];
		} else if (viewParams && viewParams[key]) {
			srfkey = viewParams[key];
		}
		Object.assign(tempContext, { [key]: srfkey });
		let result: any;
		if (
			redirectParam.redirectAction &&
			service[redirectParam.redirectAction] &&
			service[redirectParam.redirectAction] instanceof Function
		) {
			result = await service[redirectParam.redirectAction](tempContext, data);
		} else {
			result = await service.Get(tempContext, data);
		}
		const curData: any = result.data;
		// 判断当前数据模式,默认为true,todo
		const iRealDEModel: boolean = true;
		let bDataInWF: boolean = false;
		let bWFMode: any = false;
		if (redirectParam.isEnableWorkflow) {
			return this.getWFView();
		} else {
			if (
				viewParams &&
				viewParams.srfwf &&
				this.InWorkflowArray.indexOf(viewParams.srfwf) !== -1
			) {
				return this.getWFView();
			}
		}
		let strPDTViewParam: string = await this.getDESDDEViewPDTParam(
			curData,
			bDataInWF,
			bWFMode
		);
		return this.allViewMap.get(strPDTViewParam);
	}

	/**
	 * 获取实体单数据实体视图预定义参数
	 *
	 * @param curData 当前数据
	 * @param bDataInWF 是否有数据在工作流中
	 * @param bWFMode   是否工作流模式
	 * @memberof Sys_role_memberUIServiceBase
	 */
	public async getDESDDEViewPDTParam(
		curData: any,
		bDataInWF: boolean,
		bWFMode: boolean
	) {
		let strPDTParam: string = '';
		if (bDataInWF) {
			// TODO 判断数据是否在流程中
		}
		// TODO 多表单
		const multiFormDEField: string | null = null;

		const isApp = App.getProjectSetting().isApp;
		if (multiFormDEField) {
			const objFormValue: string = curData[multiFormDEField];
			if (isApp) {
				return 'MOBEDITVIEW:' + objFormValue;
			}
			return 'EDITVIEW:' + objFormValue;
		}
		if (isApp) {
			if (this.getDEMainStateTag(curData)) {
				return `MOBEDITVIEW:MSTAG:${this.getDEMainStateTag(curData)}`;
			}
			return 'MOBEDITVIEW:';
		}
		if (this.getDEMainStateTag(curData)) {
			return `EDITVIEW:MSTAG:${this.getDEMainStateTag(curData)}`;
		}
		return 'EDITVIEW:';
	}

	/**
	 * 执行界面逻辑
	 * @param logicTag 界面逻辑标识
	 * @param context 应用上下文
	 * @param viewParams 视图参数
	 * @param data 业务数据
	 * @param event 事件对象
	 * @param ability 当前环境能力
	 * @memberof Sys_role_memberUIServiceBase
	 */
	public async executeUILogic<T extends IAbility>(
		logicTag: string,
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IUIActionResult> {
		if (logicTag === 'unAuth') {
			return await UnAuthUILogic.getInstance().execute(
				context,
				viewParams,
				data[0],
				event,
				ability
			);
		}
		return { ok: true, data: null };
	}
}
